﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Office.Interop.Word;

namespace WordAddIn
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            MessageBox.Show("Hello, "+Application.UserName);
            Debug.WriteLine("插件启动");
            this.Application.DocumentOpen += DocumentOpenFunc;
            this.Application.DocumentBeforeClose += DocumentBeforeCloseFunc;
            this.Application.DocumentBeforeSave += DocumentBeforeSaveFunc;
            this.Application.DocumentChange += DocumentChangeFunc;
        }

        private void DocumentChangeFunc()
        {
            Debug.WriteLine("文档切换事件");
        }

        private void DocumentBeforeSaveFunc(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
        {
            Debug.WriteLine("文档保存前事件");
        }

        private void DocumentBeforeCloseFunc(Word.Document Doc, ref bool Cancel)
        {
            Debug.WriteLine("文档关闭前事件");
            if (Cancel)
            {
                Debug.WriteLine("取消关闭");
            }
        }

        private void DocumentOpenFunc(Word.Document Doc)
        {
            Debug.WriteLine("文档打开事件 "+Doc.Name);
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            MessageBox.Show("Goodbye,"+Application.UserName);
            Debug.WriteLine("插件关闭");
            this.Application.DocumentOpen -= DocumentOpenFunc;
            this.Application.DocumentBeforeClose -= DocumentBeforeCloseFunc;
            this.Application.DocumentBeforeSave -= DocumentBeforeSaveFunc;
            this.Application.DocumentChange -= DocumentChangeFunc;

        }

        #region VSTO 生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
